package esdclibrarypaymentsystem;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author shou1dwe
 */
class Selection {
    Control control = null;

    public Selection(){
    }

    void startService(String[] args) {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        while(true){
            displayLogin();
            Long memberId = -1L;
            try {
                String input = br.readLine();
                memberId = Long.parseLong(input);
            } catch(NumberFormatException ex){
                print("WRONG format for MEMBER ID!\n");
                continue;
            } catch (IOException ex) {
                Logger.getLogger(Selection.class.getName()).log(Level.SEVERE, null, ex);
                continue;
            }

            control = new Control(memberId);
            
            int choice = -1;
            while (choice != 0) {
                Double outstandingFine = retrievePaymentInfo(memberId);
                if(outstandingFine==null){
                    break;
                }
                displayMenu(outstandingFine);
                try {
                    String input = br.readLine();
                    choice = Integer.parseInt(input);
                } catch (IOException ex) {
                    Logger.getLogger(Selection.class.getName()).log(Level.SEVERE, null, ex);
                    continue;
                }
                executeChoice(choice);
            }
        }
    }

    private void displayLogin() {
        print("--------------------------------------------");
        print("          Library Payment Systemt:");
        print("--------------------------------------------");
        print("");
        print("Please enter your MEMBER ID: ");
    }

    private void displayMenu(Double fine) {
        print("--------------------------------------------");
        print(String.format("Outstanding Fine: %.2f", fine));
        print("--------------------------------------------");
        print("1. Make a payment, be a good child :)");
        print("0. Well well well, maybe next time ;)");
        print("--------------------------------------------");
    }

    private void executeChoice(int choice) {
        switch(choice){
            case 0:
                break;
            case 1:
                control.payFine();
                break;
            default:
                print("Unknown Choice!");
                break;
        }

    }

    private Double retrievePaymentInfo(Long memberId){
        return control.getOutstandingFine(memberId);
    }

    private void print(String string) {
        System.out.println(string);
    }


}
